package com.shiguiwu.springmybatis.javabase.sparse;

import cn.hutool.core.util.StrUtil;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @description: 稀疏数组
 * @author: stone
 * @date: Created by 2021/2/25 23:32
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.sparse
 */
public class SparseArrayTests {

    public static void main(String[] args){
        //初始化二维数组，五子棋
//        int[][] ints = initArr();
        //转为稀疏数组
//        int[][] ints1 = toSparesArr(ints);
        //存档，保存到文件中
//        save(ints1);

        //读取文件,
        String res = readFile();
        //然后转为稀疏数组
        int[][] sparesArr = stringToSparesArr(res);
        //恢复成原有数组,读档。。。
        toArray(sparesArr);
        System.out.println("================?");





    }

    public static void toArray(int[][] sparesArr) {
        int[][] ints = new int[sparesArr[0][0]][sparesArr[0][1]];
        for (int i = 1; i < sparesArr.length; i++) {
            ints[sparesArr[i][0]][sparesArr[i][1]] = sparesArr[i][2];
        }

        for (int[] row : ints) {
            for (int data : row) {
                System.out.print(data);
                System.out.print("  ");
            }
            System.out.println();
        }
    }

    public static int[][] stringToSparesArr(String res) {

        String[] arr = res.split("[ \n]");
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            if (StrUtil.isBlank(arr[i])) {
                sum++;
            }
        }
        int[][] ints = new int[sum + 1][3];

        int count = 0;
        int y = 0;
        for (int i = 0; i < arr.length; i++) {
            if (!StrUtil.isBlank(arr[i])) {
                ints[count][y] = Integer.parseInt(arr[i]);
                y++;
            }
            else {
                count++;
                y = 0;
            }
        }
        System.out.println("转为稀疏数组为：");
        for (int[] row : ints) {
            for (int data : row) {
                System.out.print(data);
                System.out.print("  ");
            }
            System.out.println();

        }
        return ints;
    }

    public static String readFile() {
        File file = null;
        BufferedInputStream inputStream = null;
        String result = null;
        try {
            file = new File("array.txt");
            inputStream = new BufferedInputStream(new FileInputStream(file));
            int read = 0;
            byte[] bytes = new byte[(int) file.length()];
            while (read != -1) {
                read = inputStream.read(bytes);
            }
            result  = new String(bytes, StandardCharsets.UTF_8);
//            System.out.println(s);

            return result;


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert inputStream != null;
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void save(int[][] ints1) {
        File file = null;
        BufferedOutputStream outputStream = null;
        try {
            file = new File("array.txt");
            boolean newFile = file.createNewFile();
            if (!newFile) {
                outputStream = new BufferedOutputStream(new FileOutputStream(file));
                for (int[] row : ints1) {
                    for (int data : row) {
                        outputStream.write((data + " ").getBytes(StandardCharsets.UTF_8));
                    }
                    outputStream.write(("\n").getBytes(StandardCharsets.UTF_8));

                }
                outputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert outputStream != null;
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static int[][] toSparesArr(int[][] ints) {
        if (ints == null) {
            return null;
        }
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (ints[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.println(String.format("有效值的个数为%s", sum));

        int[][] sparesArr = new int[sum + 1][3];
//        稀疏数组的第一行

        sparesArr[0][0] = 11;
        sparesArr[0][1] = 11;
        sparesArr[0][2] = sum;

        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (ints[i][j] != 0) {
                    count++;
                    sparesArr[count][0] = i;
                    sparesArr[count][1] = j;
                    sparesArr[count][2] = ints[i][j];
                }
            }
        }
        System.out.println("转为稀疏数组为：");
        for (int[] row : sparesArr) {
            for (int data : row) {
                System.out.print(data);
                System.out.print("  ");
            }
            System.out.println();

        }

        return sparesArr;


    }

    public static int[][] initArr() {
        int[][] arr = new int[11][11];
        arr[1][2] = 1;
        arr[2][3] = 2;

        for (int[] row : arr) {
            for (int data : row) {
                System.out.print(data);
                System.out.print("  ");
            }
            System.out.println();

        }
        return arr;
    }
}
